package com.example.appinting.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.appinting.R;
import com.example.appinting.LoginActivity;
import com.example.appinting.SQL.MysqlHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Me extends Fragment {

    private String patient_id = "0";
    private Button btn_quit,btn_about_me;
    private ImageView me_head;
    private TextView me_name;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //先拿到宿主activity 在拿到宿主中的intent传递的值
        patient_id = getActivity().getIntent().getStringExtra("patient_id");
        //测试显示是否拿到用户名
//        Toast.makeText(getActivity(),username,Toast.LENGTH_SHORT).show();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.me,container,false);
        me_name = view.findViewById(R.id.me_name);
        btn_quit = view.findViewById(R.id.my_quit);
        me_head = view.findViewById(R.id.me_head);


        me_name.setText(patientName(patient_id));
        me_head.setImageResource(R.drawable.head);

        btn_quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }
        });

        return view;
    }

    String patientName(String patientId){

        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            // 连接到 MySQL 数据库
            connection = MysqlHelper.getConnection();
            // 准备 SQL 语句
            String sql = "SELECT patient_name FROM patient WHERE patient_id = "+ patientId ;
            statement = connection.prepareStatement(sql);

            // 执行 SQL 语句
            resultSet = statement.executeQuery();
            // 处理查询结果
            if (resultSet.next()) {
                String patient_name = resultSet.getString("patient_name");
                return patient_name;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接和资源
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return "张大炮";
    }
}